<!--
 * @Author: 陈俊升 761723995@qq.com
 * @Date: 2024-01-24 10:22:02
 * @LastEditors: 陈俊升 761723995@qq.com
 * @LastEditTime: 2024-03-11 17:25:04
 * @FilePath: /hr_new_vue3/src/components/TableShow/index.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<!-- 显示表格控件转换数据 -->
<template>
  <slot name="extraColumnHeader"></slot>
  <a-table-column
    :title="item.ctlCnName"
    v-for="(item, index) in selectedHeader"
    :key="index"
    :data-index="item.ctlCode"
    :width="+JSON.parse(item.props)?.tableHeaderWidth || 200"
    :sortable="{
      sortDirections: ['ascend', 'descend']
    }"
    ellipsis
    :tooltip="{
      position: 'left'
    }"
  >
    <template #cell="{ record }">
      <ShowInfo
        :record="record"
        :ctlCode="item.ctlCode"
        v-if="!specialCtlCode?.includes(item.ctlCode)"
      />
      <slot name="custom" :cellData="record" :controlData="item"></slot>
    </template>
  </a-table-column>
</template>
<script setup lang="tsx">
import { getPositionHierarchyList } from '@/apis/management/organization/baseSetting/PositionSystem/index';
import { GetOrganizationListDetail } from '@/apis/management/organization/organizationManagement';
import {
  getPositionFlatList,
  getFlatDept,
  getAllHumanRoster,
  getPositionTypeFlatList
} from '@/utils/common';
import { GetLegalEntityList } from '@/apis/management/organization/organizationManagement/LegalEntity/index';
import { GetWorkPlaceList } from '@/apis/management/organization/organizationManagement';
import { GetJobList } from '@/apis/management/organization/positionManagement';
import { GetContractGroup } from '@/apis/management/contract/baseSetting';
import { OrganizationPatternSearch } from '@/apis/management/organization/baseSetting/organizationalType';
import { position } from 'html2canvas/dist/types/css/property-descriptors/position';
import { Tooltip } from '@arco-design/web-vue';

const props = withDefaults(
  defineProps<{
    selectedHeader: any; // 表头
    // showCtlCodeArr?: string[]; // 需要转换的编码
    specialCtlCode?: string[]; // 特殊处理的编码（例如组织名称、职位名称）在展示的页面需要缩进
  }>(),
  {}
);

const data = reactive({
  humanRosterList: [], // 人员列表
  organizationList: [], // 组织列表
  organizationTypeList: [], // 组织类型
  positionList: [], // 职位列表
  positionTypeList: [], // 职位类别列表
  positionHierarchyList: [], // 职位层级
  jobList: [], // 职务列表
  lawList: [], // 法律实体列表
  workplaceList: [], // 工作地点列表
  contractTypeList: [] // 合同类型列表
});

const pageParams = {
  pageNumber: 0,
  pageSize: -1
};
const getData = async () => {
  try {
    const [
      humanRosterListRes,
      organizationListRes,
      organizationTypeListRes,
      positionListRes,
      positionTypeListRes,
      positionHierarchyListRes,
      jobListRes,
      lawListRes,
      workplaceListRes,
      contractTypeListRes
    ] = await Promise.all([
      getAllHumanRoster(),
      getFlatDept(),
      OrganizationPatternSearch({
        ...pageParams
      }),
      getPositionFlatList(),
      getPositionTypeFlatList(),
      getPositionHierarchyList({
        ...pageParams
      }),
      GetJobList({
        ...pageParams
      }),
      GetLegalEntityList({
        ...pageParams
      }),
      GetWorkPlaceList({
        ...pageParams
      }),
      GetContractGroup({
        ...pageParams
      })
    ]);
    data.humanRosterList = humanRosterListRes;
    data.organizationList = organizationListRes;
    data.organizationTypeList = organizationTypeListRes.content;
    data.positionList = positionListRes;
    data.positionTypeList = positionTypeListRes;
    data.positionHierarchyList = positionHierarchyListRes.content;
    data.jobList = jobListRes.content;
    data.lawList = lawListRes.content;
    data.workplaceList = workplaceListRes.content;
    data.contractTypeList = contractTypeListRes.content;
  } catch (error: unknown) {
    console.log(error);
  }
};
getData();

// 处理特殊渲染的数据
const ShowInfo = (props: any) => {
  const record = props.record;
  const ctlCode = props.ctlCode;
  switch (ctlCode) {
    // 组织名称， 公司，部门，兼任部门，兼任公司,上级组织
    case 'C30BBF36A5AF541F7A2657671DEE2EDD6':
    case 'CA8ABCC1480744557B3014C3454465A7E':
    case 'CED44AC9966904CD1AB546D309903727C':
    case 'C93B151DFAF7743A5848328BCA0CFC334':
    case 'CA43B2FD3CE4245B2891D234343E38697':
    case 'C073F4F41C46D4CA3AF3BEC1F51A8F410':
      return (
        <>
          {data.organizationList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    // 职位, 兼职职位
    case 'C7341027925C943708F3AEF98703ECC47':
    case 'C76AD0D3D6B3C45169C250A37B71870EF':
      return (
        <>
          {data.positionList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    // 职务,关联职务,兼任职务
    case 'C1F5AA49A78EC4B4FBD487F4D5887E576':
    case 'C6E3A2A00BCDC44B785F253C3940B4D75':
    case 'C0AC7334EB7424A08AB02E8FE2CF194CC':
      return (
        <>
          {data.jobList.find((item: any) => item.id == record[ctlCode])?.name ||
            '---'}
        </>
      );
    // 职位层级
    case 'C9A9EF3DFFFC84AAFB97443DEE8E5B8CC':
      return (
        <>
          {data.positionHierarchyList.find(
            (item: any) => item.id == record[ctlCode]
          )?.name || '---'}
        </>
      );
    // 出生日期、入职时间
    case 'C4869FD6FEC404979A6F73A14DFA07E78':
    case 'CC0D5415792B24BBCAD7DF5AB8A25A0AB':
      return (
        <>
          {/* {ctlCode == 'C4869FD6FEC404979A6F73A14DFA07E78'
            ? record['C4869FD6FEC404979A6F73A14DFA07E78']?.slice(2, -2)
            : record['CC0D5415792B24BBCAD7DF5AB8A25A0AB']?.slice(2, -2)} */}
          {record[ctlCode]?.slice(2, -2)}
        </>
      );
    // 合同公司
    case 'C8258F2F3F7EC4D9C803A291BB7BFEE28':
      return (
        <>
          {data.lawListRes.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    // 合同类型
    case 'C05110D9921B749C4A15D075E79C7F5B8':
      return (
        <>
          {data.contractTypeList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    // 直接上级,组织负责人，分管领导
    case 'CECEBAA743F9B4160B40A5A3E78588712':
    case 'CB0C63F38283045F58FFF1B1EABE5D151':
    case 'CA6BD9CC1C99D4D13AD4F5009EF4F9586':
      return (
        <>
          {data.humanRosterList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    //组织类型
    case 'C3D33FA57216F4968BDE51C31D623FC09':
      return (
        <>
          {data.organizationTypeList.find(
            (item: any) => item.id == record[ctlCode]
          )?.name || '---'}
        </>
      );
    // 工作地点
    case 'CE009C2E2B542409D815F0D3D54FE0E2B':
      return (
        <>
          {data.workplaceList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    // 职位类别
    case 'C63EC32DCE9CC4BE6AC7649E23234A542':
      return (
        <>
          {data.positionTypeList.find((item: any) => item.id == record[ctlCode])
            ?.name || '---'}
        </>
      );
    default:
      return (
        <>
          {record[ctlCode] && record[ctlCode] != 'undefined'
            ? record[ctlCode]
            : '---'}
        </>
      );
  }
};
</script>
<style lang="less" scoped></style>
